/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */
package com.yunbiji.controller;

import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.yunbiji.interceptor.RegisterInterceptor;
import com.yunbiji.interceptor.SessionInterceptor;
import com.yunbiji.model.Users;
import com.yunbiji.validate.ChangePasswordValidate;
@Before(SessionInterceptor.class)
/**
 * 成员模块操作类
 * @author 聂道鑫
 *
 */
public class UserProfileController extends Controller
{
	private static final Logger logger = Logger.getLogger(UserProfileController.class);
	
	public void index()
	{
		render("modal/navigation.html");
	}
/**
 * 返回主界面
 */
	public void mem()
	{
		render("member.html");
	}
	/**
	 * 点击个人中心获得个人资料
	 */
	public void getUserProfile()
	{
		String tel=getSessionAttr("username");
		List<Users> list =Users.dao.getUserProfile(tel);
		setAttr("userProfileList",list);
		render("modal/navigation.html");
	}
	/**
	 * 修改个人信息
	 */
	public void updateUserProfile()
	{
		String tel=getSessionAttr("username");
		
		String a_username=getPara("a_username");
		String a_job=getPara("a_job");
		String a_birth=getPara("a_birth");
		String a_email=getPara("a_email");
		String a_qq=getPara("a_qq");
		boolean flag=Users.dao.updateUserProfile(a_username, a_job, a_birth, a_email, a_qq, tel);
		if(flag)
		{
			List<Users> list =Users.dao.getUserProfile(tel);
			setAttr("userProfileList",list);
			render("modal/navigation.html");
		}
		else
		{
			logger.error("updateUserProfile方法出现错误");
			setAttr("alertInfo", "个人信息修改失败！");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "2;URL=index");
		}
		
	}
	/**
	 * 修改密码
	 */
	@Before(ChangePasswordValidate.class)
	public void changePassword()
	{
		String tel=getSessionAttr("username");
		
		String oldPasswd=getPara("oldPasswd");
		String changePasswd=getPara("changePasswd");
		
		boolean flag=Users.dao.changePassword(oldPasswd, changePasswd, tel);
		
		if(flag)
		{
			setAttr("alertInfo", "修改密码成功！");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "2;URL=getUserProfile");
		}
		else
		{
			logger.error("changePassword方法出现错误");
			setAttr("alertInfo", "修改密码失败！");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "2;URL=getUserProfile");
		}
		
	}
	/**
	 * 点击下一页的时候，进行分页
	 * @author niedaoxin
	 */
	public void pageMem()
	{
		// 确认详细结果分页
		Integer pagenoMem = getParaToInt(0);;
		String tel = getSessionAttr("username");
		if (null == pagenoMem || pagenoMem < 1)
			{
				pagenoMem = 1;
			}
		String pIdMem=getSessionAttr("pIdMem");
		List<Users> list = Users.dao.getUsers(tel, pIdMem, pagenoMem);
		setSessionAttr("pagenoMem",pagenoMem);
		setAttr("pagenoMem", pagenoMem);
		setAttr("getMembers_Members", list);
		
		render("member.html");
	}
	
	/**
	 * 新增成员功能
	 */
	public void newMem()
	{
		try
		{
			String pIdMem=getSessionAttr("pIdMem");
			String memName=getPara("memName");
			String tel=getSessionAttr("username");
			
			Users.dao.updateUsers(tel, memName, pIdMem);
			
			int idMem=Users.dao.queryIntId(tel);
			setSessionAttr("idMem", idMem);
			// 确认详细结果分页
			Integer pagenoMem = getSessionAttr("pagenoMem");
			List<Users> list = Users.dao.getUsers(tel, pIdMem, pagenoMem);
			setAttr("pagenoMem", pagenoMem);
			setAttr("getMembers_Members", list);
			render("member.html");
		}
		catch(NumberFormatException e)
		{
			logger.error("newMem方法出现异常");
			setAttr("alertInfo", "你没有选择文件夹，请先选中文件夹！");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "2;URL=mem");
		}

	}
	/**
	 * 删除选中的成员
	 */
	public void deleteMem()
	{
		try
		{
			String tel=getSessionAttr("username");
			int idMem=getSessionAttr("idMem");
			Users.dao.deleteById(idMem);
			
			// 确认详细结果分页
			String pIdMem=getSessionAttr("pIdMem");
			Integer pagenoMem = getSessionAttr("pagenoMem");
			List<Users> list = Users.dao.getUsers(tel, pIdMem, pagenoMem);
			setAttr("pagenoMem", pagenoMem);
			setAttr("getMembers_Members", list);
			render("member.html");
		}
		catch(NullPointerException e)
		{
			logger.error("deleteMem出现异常");
			setAttr("alertInfo", "你没有选择要删除的成员，请先选中成员！");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "2;URL=mem");
		}

	}
	/**
	 * 点击成员的名字来显示成员的个人信息
	 */
	public void showProfile()
	{
		int idMem=getParaToInt(0);
		setSessionAttr("idMem", idMem);
		String tel=getSessionAttr("username");
		Record theUser=Users.dao.getMembersById(idMem);
		
		setAttr("contentMem",theUser.get("a_text"));
		
		setAttr("usernameMemValue",theUser.get("a_username"));
		setAttr("accountMemValue",theUser.get("id"));
		setAttr("passwordMemValue",theUser.get("a_userpass"));
		setAttr("repasswordMemValue",theUser.get("a_userpass"));
		setAttr("workMemValue",getSessionAttr("pNameMem"));
		setAttr("telMemValue",theUser.get("tel"));
		setAttr("birthMemValue",theUser.get("a_birth"));
		setAttr("emailMemValue",theUser.get("a_email"));
		setAttr("qqMemValue",theUser.get("a_qq"));
		
	if((int)theUser.get("a_allowlogin")==1)
		{	
			setAttr("allowMemValue", "是");
			setAttr("valueMem", (int)theUser.get("a_allowlogin"));
			
		}
		else
		{
			setAttr("allowMemValue", "否");
			setAttr("valueMem", (int)theUser.get("a_allowlogin"));
		}
		
		
		setSessionAttr("memName", theUser.get("a_username"));
						
		// 确认详细结果分页
		int pIdMem=theUser.getInt("a_folder");
		Integer pagenoMem = getSessionAttr("pagenoMem");
		List<Users> list = Users.dao.getUsers(tel, String.valueOf(pIdMem), pagenoMem);
		setAttr("pagenoMem", pagenoMem);
		setAttr("getMembers_Members", list);
		render("member.html");
	}
	/**
	 * 保存修改后的员工信息
	 */
	@Before(RegisterInterceptor.class)
	public void updateProfile()
	{
		String usernameMem=getPara("usernameMem");
		String passwordMem=getPara("passwordMem");
		String repasswordMem=getPara("repasswordMem");
		String telMem=getPara("telMem");
		String birthMem=getPara("birthMem");
		String emailMem=getPara("emailMem");
		String qqMem=getPara("qqMem");
		String allowMem=getPara("allowMem");
		String contentMem =getPara("contentMem");
		
		
		int idMem=getSessionAttr("idMem");;
		try
		{
			if(!passwordMem.equals(repasswordMem))
			{
				keepPara("usernameMemValue","accountMemValue","workMemValue","telMemValue","birthMemValue","emailMemValue","qqMemValue");
				setAttr("errorMem", "两次密码不相同请重新输入!");
			}
			else if(passwordMem.length()<6)
			{
				keepPara("usernameMemValue","accountMemValue","workMemValue","telMemValue","birthMemValue","emailMemValue","qqMemValue");
				setAttr("errorMem", "请使用字母和数字。密码长度要大于5位小于16位!");
			}
			else
			{
				
				if(passwordMem.equals(Users.dao.getOldString(idMem)))
				{
					Users.dao.updateUserProfileMemNoChangePwd(usernameMem, telMem, birthMem, emailMem, qqMem, allowMem, contentMem, idMem);
				}
				else
				{
					Users.dao.updateUserProfileMem(usernameMem, passwordMem, telMem, birthMem, emailMem, qqMem, allowMem,contentMem,idMem);
				}
				
				
			}
			this.pageComm();
		}
		catch(Exception e)
		{
			logger.error("updateProfile方法出现异常");
			setAttr("alertInfo", "修改信息出错！请重新修改.2秒后自动返回成员界面！");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "2;URL=mem");
		}
		
		
	}
	/**
	 * 基本分页程序
	 */
	public void pageComm()
	{
		// 确认详细结果分页
		String tel=getSessionAttr("username");
		String pIdMem=getSessionAttr("pIdMem");
		Integer pagenoMem = getSessionAttr("pagenoMem");
		List<Users> list = Users.dao.getUsers(tel, pIdMem, pagenoMem);
		setAttr("pagenoMem", pagenoMem);
		setAttr("getMembers_Members", list);
		render("member.html");
	}
}
